Home:ALL Converter>Outh2 Access Token Exception

Outh2 Access Token Exception

Ask Time:2020-02-24T01:10:51         Author:amitabh pandey

Json Formatter

I am implementing Outh2 for authentication in spring boot application.I am getting the authorization code successfully but when i am making post request to token url by rest template it is giving me exception 400 bad Request.By this exception i am not able to identify the issue.Below is my code.

        ResponseEntity<String> response = null;
        System.out.println("Authorization Ccode------" + code);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> request = new HttpEntity<String>(headers);
        String access_token_url = "https://www.googleapis.com/oauth2/v3/token";
        access_token_url += "?code=" + code;
        access_token_url += "&client_id =487786082511-ta7fnptb8dnd4lbq9lphbtbpll9eo1it.apps.googleusercontent.com";
        access_token_url += "&client_secret  =fS4KHLUUMOm-lYu8QtDOFcDV";
        access_token_url += "&grant_type=authorization_code";
        access_token_url += "&redirect_uri=http://localhost:8080/salesgoal/googleOuth2Success";
        try {
        response = restTemplate.exchange(access_token_url, HttpMethod.POST, request, String.class);
        }
        catch(Exception e){

            e.printStackTrace();

Please let me know what i am doing wrong. Thanks

Author:amitabh pandey,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364718/outh2-access-token-exception
yy